const getters = {
  cityList: (state) => state.area.cityList,
  areaList: (state) => state.area.areaList,
  areaSourceList: (state) => state.area.areaSourceList,
  companyEmployeeList: (state) => state.company.companyEmployeeList,
  companyAdminList: (state) => state.company.companyAdminList,
  companyRoleList: (state) => state.company.companyRoleList,
  companyPositionList: (state) => state.company.companyPositionList,
  companyDepartmentList: (state) => state.company.companyDepartmentList,
  sidebar: (state) => state.app.sidebar,
  name: (state) => state?.auth?.userInfo?.name || "",
  dialogs: (state) => state.wages.dialogs,
  device: (state) => state.app.device,
  token: (state) => state.auth.token,
  avatar: (state) => state.auth.avatar,
  permissions: (state) => state.auth.permissions,
  permissions_menu: (state) => {
    if (state.auth.permissions && state.auth.permissions.menuList) {
      return state.auth.permissions.menuList;
    }
    return [];
  },
  permissions_menu_urls: (state) => {
    if (state.auth.permissions && state.auth.permissions.menuList) {
      return state.auth.permissions.menuList.map((item) => item.url);
    }
    return [];
  },
  permissions_menu_tree: (state) => {
    if (state.auth.permissions && state.auth.permissions.menuList) {
      return toTree(state.auth.permissions.menuList);
    }
    return [];
  },
  permissions_handle: (state) => {
    if (state.auth.permissions && state.auth.permissions.permissionStringList) {
      return state.auth.permissions.permissionStringList.filter((handle) => handle.includes("hr:"));
    }
    return [];
  },
  permissions_dycl: (state) => {
    if (state.auth.permissions && state.auth.permissions.permissionStringList) {
      return state.auth.permissions.permissionStringList.filter((handle) => handle.includes("dycl:"));
    }
    return [];
  },
  permissions_shangbao: (state) => {
    if (state.auth.permissions && state.auth.permissions.permissionStringList) {
      return state.auth.permissions.permissionStringList.filter((handle) => handle.includes("insuranceAddService:"));
    }
    return [];
  },
  permissions_site: (state) => {
    if (state.auth.permissions && state.auth.permissions.siteList) {
      return state.auth.permissions.siteList;
    }
    return [];
  },
  permissions_type: (state) => {
    if (state.auth.permissions && state.auth.permissions.subjectTypeList) {
      return state.auth.permissions.subjectTypeList;
    }
    return [];
  },
  isJF: (state) => {
    if (state.auth.permissions && state.auth.permissions.siteList) {
      const siteList = state.auth.permissions.siteList;
      const currentSite = siteList.filter((item) => item.currSite)[0];
      return [3, 4].includes(currentSite.siteType);
    }
    return false;
  }
};

export default getters;

function toTree(data) {
  data.forEach((item) => {
    delete item.children;
  });
  const map = {};
  data.forEach((item) => {
    map[item.id] = item;
  });
  const val = [];
  data.forEach((item) => {
    const parent = map[item.parentId];
    if (!!parent) {
      (parent.children || (parent.children = [])).push(
        Object.assign(item, {
          title: item.title || item.name,
          key: `${item.id}&&${item.title || item.name}&&${item.isDirectory}`
        })
      );
    } else {
      val.push(
        Object.assign(item, {
          title: item.title || item.name,
          key: `${item.id}&&${item.title || item.name}&&${item.isDirectory}`
        })
      );
    }
  });
  return val;
}
